package com.mes.inventory.dao;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.bstek.dorado.data.provider.Page;
import com.mes.common.dao.BasicDao;
import com.mes.inventory.dao.iface.IInvlnfoDao;
import com.mes.inventory.domain.InvInfo;


@Repository
public class InvInfoDao extends BasicDao<InvInfo> implements IInvlnfoDao{

	@Override
	public void getInvInfoByCondition(Page<InvInfo> page, Map<String, Object> params) throws Exception {
		Map<String, Object> args = new HashMap<String, Object>();
		StringBuffer coreHql = new StringBuffer("from InvInfo a where 1=1 ");

		//仓库
		String warehouseUid = (String) params.get("warehouseUid");
		if (StringUtils.isNotEmpty(warehouseUid)) {
			coreHql.append(" and a.warehouseUid =:warehouseUid ");
			args.put("warehouseUid", warehouseUid );
		}
		
		//柜位
		String warebinUid = (String) params.get("warebinUid");
		if (StringUtils.isNotEmpty(warebinUid)) {
			coreHql.append(" and a.warebinUid =:warebinUid ");
			args.put("warebinUid", warebinUid);
		}
		
		//图号
		String partDrawingId = (String) params.get("partDrawingId");
		if (StringUtils.isNotEmpty(partDrawingId)) {
			coreHql.append(" and a.partDrawingId =:partDrawingId ");
			args.put("partDrawingId", partDrawingId);
		}
		
		//名称
		String partName = (String) params.get("partName");
		if (StringUtils.isNotEmpty(partName)) {
			coreHql.append(" and a.partName =:partName ");
			args.put("partName", partName);
		}
		
		//规格
		String partDescription = (String) params.get("partDescription");
		if (StringUtils.isNotEmpty(partDescription)) {
			coreHql.append(" and partDescription =:partDescription ");
			args.put("partDescription",partDescription);
		}
		
		//批次号
		String batchNum = (String) params.get("batchNum");
		if (StringUtils.isNotEmpty(batchNum)) {
			coreHql.append(" and batchNum =:batchNum ");
			args.put("batchNum",batchNum);
		}
		
		//零件唯一号
		String partUid = (String) params.get("partUid");
		if (StringUtils.isNotEmpty(partUid)) {
			coreHql.append(" and partUid =:partUid ");
			args.put("partUid",partUid);
		}
		
		//是否封存
		
		/*boolean isFreezed = (boolean) params.get("isFreezed");
			coreHql.append(" and isFreezed =:isFreezed ");
			args.put("isFreezed",isFreezed);*/
		
		String isFreezed1 = (String) params.get("isFreezed");
		if (StringUtils.isNotEmpty(isFreezed1)) {
			
			boolean isFreezed = false;
			if (isFreezed1.equals("已封存")) {
				isFreezed = true;
			}
			coreHql.append(" and isFreezed =:isFreezed ");
			args.put("isFreezed", isFreezed);
		}
			
		
		String countHql = "select count(*) " + coreHql.toString();
		String hql = coreHql.toString();
		
		Map<String, Object> newArgs = new HashMap<String, Object>();
		String key;
		String newKey;
		Iterator<String> iterator = args.keySet().iterator();
		while(iterator.hasNext()){
			key = (String)iterator.next();
			newKey = key.replace(".", "_");
			newArgs.put(newKey, args.get(key));
			hql = hql.replace(":"+key, ":"+newKey);
			countHql = countHql.replace(":"+key, ":"+newKey);
		}
		this.pagingQuery(page, hql, countHql, newArgs);
		
	}

	@Override
	public InvInfo getInvInfo(String materialUid, String warehouseUid, String warebinUid) {
		String hql = "from InvInfo a where a.materialUid =:materialUid and "
				+ "warehouseUid =:warehouseUid and warebinUid =:warebinUid";
		
		Map<String, Object> args = new HashMap<>();
		args.put("materialUid", materialUid);
		args.put("warehouseUid", warehouseUid);
		args.put("warebinUid", warebinUid);
 		
		List<InvInfo> list = this.query(hql, args);
		if(list !=null && list.size() > 0){
			return list.get(0);
		}else{
			return null;
		}
	}
	@Override
	public void getInvInfo(Page<InvInfo> page, Map<String, Object> parameter) throws Exception {

		// TODO Auto-generated method stub
		Map<String, Object> args = new HashMap<String, Object>();

		StringBuffer coreHql = new StringBuffer("from " + InvInfo.class.getName() + " a where 1=1 ");

		String warehouseUid = (String) parameter.get("warehouseUid");
		if (StringUtils.isNotEmpty(warehouseUid)) {
			coreHql.append(" and a.warehouseUid =:warehouseUid ");
			args.put("warehouseUid", warehouseUid);
		}
		String countHql = "select count(*) " + coreHql.toString();
		String hql = coreHql.toString();

		Map<String, Object> newArgs = new HashMap<String, Object>();
		String key;
		String newKey;
		Iterator<String> iterator = args.keySet().iterator();
		while (iterator.hasNext()) {
			key = (String) iterator.next();
			newKey = key.replace(".", "_");
			newArgs.put(newKey, args.get(key));
			hql = hql.replace(":" + key, ":" + newKey);
			countHql = countHql.replace(":" + key, ":" + newKey);

			System.out.println("hql: " + hql);
			System.out.println("countHql: " + countHql);
			System.out.println("newArgs: " + newArgs);
		}
		this.pagingQuery(page, hql, countHql, newArgs);

	}
	
	//模糊查询
	@Override
	public void getInvInfos(Page<InvInfo> page, Map<String, Object> params) throws Exception {
		Map<String, Object> args = new HashMap<String, Object>();
		StringBuffer coreHql = new StringBuffer("from InvInfo a where 1=1 ");
		// 图号
		String partDrawingId = (String) params.get("partDrawingId");
		if (StringUtils.isNotEmpty(partDrawingId)) {
			coreHql.append(" and a.partDrawingId like :partDrawingId ");
			args.put("partDrawingId", "%" + partDrawingId + "%");
		}
		//批次
		String batchNum = (String) params.get("batchNum");
		if (StringUtils.isNotEmpty(batchNum)) {
			coreHql.append(" and a.batchNum like :batchNum ");
			args.put("batchNum", "%" + batchNum + "%");
		}
		String countHql = "select count(*) " + coreHql.toString();
		String hql = coreHql.toString();

		Map<String, Object> newArgs = new HashMap<String, Object>();
		String key;
		String newKey;
		Iterator<String> iterator = args.keySet().iterator();
		while (iterator.hasNext()) {
			key = (String) iterator.next();
			newKey = key.replace(".", "_");
			newArgs.put(newKey, args.get(key));
			hql = hql.replace(":" + key, ":" + newKey);
			countHql = countHql.replace(":" + key, ":" + newKey);
		}
		this.pagingQuery(page, hql, countHql, newArgs);

	}
		

}
